package com.zrrd.yt.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yt.common.result.Result;
import com.zrrd.yt.model.product.SpuInfo;
import com.zrrd.yt.model.product.SpuSaleAttr;
import com.zrrd.yt.product.service.SkuInfoService;
import com.zrrd.yt.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SkuInfoService skuInfoService;

    //分页
    @GetMapping("/{page}/{limit}")
    public Result<Page> page(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit,
                             Long category3Id){
        //创建分页对象
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        //创建条件构造器
        QueryWrapper<SpuInfo> spuInfoQueryWrapper = new QueryWrapper<>();
        spuInfoQueryWrapper.eq("category3_id",category3Id);
        //调用方法实现分页查询
        spuInfoService.page(spuInfoPage,spuInfoQueryWrapper);
        return Result.ok(spuInfoPage);

    }
    //保存spu
    @PostMapping("/saveSpuInfo")
    public Result<String> saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok("成功");
    }

    //查询spu的销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId){
        return Result.ok(skuInfoService.getSpuSaleAttrList(spuId));
    }

}
